package net.gzw1993.runmate.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.mob.tools.utils.UIHandler;

import net.gzw1993.runmate.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

public class EnterActivity extends BaseActivity implements PlatformActionListener,Handler.Callback {
    @ViewInject(R.id.enter_start_phone)
    private TextView phone;
    @ViewInject(R.id.enter_weixin)
    private TextView weixin;
    @ViewInject(R.id.enter_qq)
    private TextView qq;
    @ViewInject(R.id.enter_xinlang)
    private TextView sina;

    private static final int MSG_USERID_FOUND = 1;
    private static final int MSG_LOGIN = 2;
    private static final int MSG_AUTH_CANCEL = 3;
    private static final int MSG_AUTH_ERROR= 4;
    private static final int MSG_AUTH_COMPLETE = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);
        setContentView(R.layout.activity_enter);
        ViewUtils.inject(this,this);

    }


    @OnClick({R.id.enter_start_phone,R.id.enter_weixin,R.id.enter_qq,R.id.enter_xinlang})
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.enter_weixin:
                Platform platWeixin = ShareSDK.getPlatform(this, Wechat.NAME);
                platWeixin.SSOSetting(true);
                WechatAuthorize(platWeixin);
                break;
            case R.id.enter_qq:
                Platform platQq = ShareSDK.getPlatform(this, QQ.NAME);
                platQq.SSOSetting(true);
                WechatAuthorize(platQq);
                break;
            case R.id.enter_xinlang:
                Platform platformSina = ShareSDK.getPlatform(this, SinaWeibo.NAME);
                WechatAuthorize(platformSina);
                break;
            case R.id.enter_start_phone:
                
                break;
        }
    }

    public void WechatAuthorize(Platform platform){
        if(platform.isValid()){
            String userId = platform.getDb().getUserId();
            if (!TextUtils.isEmpty(userId)) {
                UIHandler.sendEmptyMessage(MSG_USERID_FOUND, this);
                login(platform.getName(), userId, null);
                return;
            }
        }
        platform.setPlatformActionListener(this);
        platform.SSOSetting(true);
        platform.showUser(null);
    }

    private void login(String plat, String userId, HashMap<String, Object> userInfo) {
        Message msg = new Message();
        msg.what = MSG_LOGIN;
        msg.obj = plat;
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
        if (action == Platform.ACTION_USER_INFOR) {
            UIHandler.sendEmptyMessage(MSG_AUTH_COMPLETE, this);
            login(platform.getName(), platform.getDb().getUserId(), hashMap);
        }
    }

    @Override
    public void onError(Platform platform, int action, Throwable throwable) {
        if (action == Platform.ACTION_USER_INFOR) {
            UIHandler.sendEmptyMessage(MSG_AUTH_ERROR, this);
        }
        throwable.printStackTrace();
    }

    @Override
    public void onCancel(Platform platform, int action) {
        if (action == Platform.ACTION_USER_INFOR) {
            UIHandler.sendEmptyMessage(MSG_AUTH_CANCEL, this);
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch(msg.what) {
            case MSG_USERID_FOUND: {
            }
            break;
            case MSG_LOGIN: {
                String text = getString(R.string.logining, msg.obj);
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                finish();
            }
            break;
            case MSG_AUTH_CANCEL: {
                Toast.makeText(this, R.string.auth_cancel, Toast.LENGTH_SHORT).show();
            }
            break;
            case MSG_AUTH_ERROR: {
                Toast.makeText(this, R.string.auth_error, Toast.LENGTH_SHORT).show();
            }
            break;
            case MSG_AUTH_COMPLETE: {
                Toast.makeText(this, R.string.auth_complete, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                finish();
            }
            break;
        }
        return false;
    }
    protected void onDestroy() {
        ShareSDK.stopSDK(this);
        super.onDestroy();
    }
}
